package de.unidue.langtech.teaching.pp.example;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;

import ude.DetectedSentiment;
import ude.GivenSentiment;

public class EvaluatorTwitter
    extends JCasAnnotator_ImplBase
{
    private float correct;
    private float nrOfDocuments;
    private float nrOfPositives;
    private float nrOfNegatives;
    private float nrOfNeutrals;
    private float nrOfDetectedPositives;
    private float nrOfDetectedNegatives;
    private float nrOfDetectedNeutrals;
    
    /* 
     * This is called BEFORE any documents are processed.
     */
    @Override
    public void initialize(UimaContext context)
        throws ResourceInitializationException
    {
        super.initialize(context);
        correct = 0;
        nrOfDocuments = 0;
        nrOfDetectedPositives = 0;
        nrOfPositives = 0;
        nrOfDetectedNegatives = 0;
        nrOfNegatives = 0;
        nrOfDetectedNeutrals = 0;
        nrOfNeutrals = 0;
    }
    
    
    /* 
     * This is called ONCE for each document
     */
    @Override
    public void process(JCas jcas)
        throws AnalysisEngineProcessException
    {
        nrOfDocuments++; 
        
        DetectedSentiment detected = JCasUtil.selectSingle(jcas, DetectedSentiment.class);
        GivenSentiment actual = JCasUtil.selectSingle(jcas, GivenSentiment.class);
        
        // sets "objective-OR-neutral" and "objective" = "neutral"
        System.out.println(actual.getSentiment() + " detected as " + detected.getSentiment());
        if (detected.getSentiment().equals(actual.getSentiment()) || 
  		 (detected.getSentiment().equals("\"neutral\"")) && actual.getSentiment().equals("\"objective-OR-neutral\"") || 
		    (detected.getSentiment().equals("\"neutral\"")) && actual.getSentiment().equals("\"objective\"")) {
            correct++;
        }
        // total postive Tweets and number of correct positive tweets are counted;
        // Control: System.out.println("Total positives " + nrOfPositives + "Number of detected positives " + nrOfDetectedPositives);
        if (actual.getSentiment().equals("\"positive\""))
        		{
        	nrOfPositives++;
        		}
        if (detected.getSentiment().equals("\"positive\"") && (actual.getSentiment().equals("\"positive\"")))
		{
        	nrOfDetectedPositives++;	
		}
        // total negative Tweets and number of correct negative tweets are counted;
        // Control: System.out.println("Total negatives " + nrOfNegatives + "Number of detected negatives " + nrOfDetectedNegatives);
        if (actual.getSentiment().equals("\"negative\""))
        		{
        	nrOfNegatives++;
        		}
        if (detected.getSentiment().equals("\"negative\"") && (actual.getSentiment().equals("\"negative\"")))
		{
        	nrOfDetectedNegatives++;  	
		}
        // total neutral Tweets and number of correct neutral tweets are counted;
        // Control: System.out.println("Total neutrals " + nrOfNeutrals + "Number of detected neutrals " + nrOfDetectedNeutrals);
        if (actual.getSentiment().equals("\"neutral\""))
        		{
        	nrOfNeutrals++;
        		}
        if (detected.getSentiment().equals("\"neutral\"") && (actual.getSentiment().equals("\"neutral\"")))
		{
        	nrOfDetectedNeutrals++;
		}
    }
    
    /* 
     * This is called AFTER all documents have been processed.
     */
    @Override
    public void collectionProcessComplete()
        throws AnalysisEngineProcessException
    {
        super.collectionProcessComplete();
        
        float accuracy = (correct / nrOfDocuments) * 100;
        float precisionPositive = (nrOfDetectedPositives / nrOfPositives) * 100;
        float precisionNegative = (nrOfDetectedNegatives / nrOfNegatives) * 100;
        float precisionNeutral = (nrOfDetectedNeutrals / nrOfNeutrals) * 100;
        
        System.out.println( "---------------------------------------------------");
        System.out.println( "Positive Precision = " + precisionPositive  + "%");
        System.out.println( "Negative Precision = " + precisionNegative  + "%");
        System.out.println( "Neutral Precision = " + precisionNeutral  + "%");
        System.out.println(correct + " out of " + nrOfDocuments + " are correct.");
        System.out.println(accuracy + " % are detected correctly.");
    }
}